﻿using System;
using System.Collections.Generic;

namespace HobSoft.SoccerBet2014.Common.Collection
{
    using Errors;
    public class LambdaComparer<T> : IComparer<T>
    {
        private readonly Func<T, T, int> _lambdaComparer;
        public LambdaComparer(Func<T, T, int> lambdaComparer)
        {
            lambdaComparer.IsNull().ThrowNull("lambdaComparer");
            _lambdaComparer = lambdaComparer;
        }

        public int Compare(T x, T y)
        {
            return _lambdaComparer(x, y);
        }
    }

    public class LambdaEqualityComparer<T> : IEqualityComparer<T>
    {
        private readonly Func<T, T, bool> _lambdaEqComparer;
        private readonly Func<T, int> _lambdaHash;

        public LambdaEqualityComparer(Func<T, T, bool> lambdaComparer) :
            this(lambdaComparer, o => o.GetHashCode())
        {
        }

        public LambdaEqualityComparer(Func<T, T, bool> lambdaEqComparer, Func<T, int> lambdaHash)
        {
            lambdaEqComparer.IsNull().ThrowNull("lambdaEqComparer");
            lambdaHash.IsNull().ThrowNull("lambdaHash");

            _lambdaEqComparer = lambdaEqComparer;
            _lambdaHash = lambdaHash;
        }


        public bool Equals(T x, T y)
        {
            return _lambdaEqComparer(x, y);
        }

        public int GetHashCode(T obj)
        {
            return _lambdaHash(obj);
        }
    }
}
